﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using DevComponents.AdvTree;
using NanoERP.Model;
using System.Collections.Generic;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    //[Module("FrmSelectStaff")]
    public partial class FrmSelectStorage : ModuleForm
    {
        readonly SysdictableBLL _sysdictablebLl;
        readonly StorageinfoBLL _storageinfoBLL;
        readonly Dictionary<long, string> _dicSelected;

        /// <summary>
        /// 仓库信息
        /// </summary>
        public FrmSelectStorage()
        {
            InitializeComponent();

            _storageinfoBLL = new StorageinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _dicSelected = new Dictionary<long, string>();

            BindTreeView();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            var node = dicTree.SelectedNode;
            if (node == null) return;

            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _storageinfoBLL.Query(int.Parse(node.Name), txtKey.Text.Trim());
        }

        /// <summary>
        /// 绑定仓库类别树
        /// </summary>
        void BindTreeView()
        {
            dicTree.Nodes.Clear();

            var sysdic = _sysdictablebLl.Get((int)SysDicEnum.仓库类别);
            var firNode = new Node(sysdic.Dicname) { Tag = sysdic, Name = sysdic.Id.ToString(), Image = Properties.Resources.XmlStructure };
            firNode.ExpandAll();
            var firList = _sysdictablebLl.Query(sysdic.Id);
            RecursiveLoopNodes(firList, firNode);

            dicTree.Nodes.Add(firNode);
            if (dicTree.Nodes.Count > 0)
                dicTree.SelectedNode = dicTree.Nodes[0].Nodes.Count > 0 ? dicTree.Nodes[0].Nodes[0] : dicTree.Nodes[0];
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="parentNode">上级节点</param>
        void RecursiveLoopNodes(List<Sysdictable> firList, Node parentNode)
        {
            firList.ForEach(r =>
            {
                var secNode = new Node(r.Dicname) { Tag = r, Name = r.Id.ToString(), Image = Properties.Resources.CreateTableTemplatesGallery };

                parentNode.Nodes.Add(secNode);
                var recList = _sysdictablebLl.Query(r.Id);
                if (recList.Count > 0)
                    RecursiveLoopNodes(recList, secNode);
            });
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null)
            {
                MessageBox.Show(@"没有选择仓库，请重新检索后选择记录！", @"提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var vv = dgvList.CurrentRow.DataBoundItem as Storageinfo;
            _dicSelected.Add(vv.Id, vv.Stoname);

            this.Tag = _dicSelected;
            this.DialogResult = DialogResult.OK;
        }

        private void dicTree_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            QueryInfo();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            btnSelect_Click(null, null);
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var obj = dicTree.SelectedNode.Tag as Sysdictable;
            var f = new FrmStorageinfoEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            QueryInfo();
        }
    }
}
